﻿using SuperX.Common.PlugIn;
using SuperX.Common.Respository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace SuperX.Common.BasicService
{
    // 
    public class BasicServiceManager
    {
        // 
        public static IProjService GetProjService()
        {
            if (_serverHost == null)
            {
                var dllPath = Path.Combine(Environment.CurrentDirectory, "Super.ProjectCore.dll");
                if (!File.Exists(dllPath))
                {
                    return null;
                }
                MethodInfo method = Assembly.LoadFrom(dllPath).GetType("Super.ProjectCore.ProjService")
                    .GetMethod("GetInstance", BindingFlags.Static | BindingFlags.Public);
                _serverHost = method?.Invoke(null,new object[] { false}) as IProjService;
            }
            return _serverHost;
        }

        // 
        public static void SetServerHost(IProjService server)
        {
            _serverHost = server;
        }

        // 
        public static string GetStartupProject()
        {
            return BasicServiceManager.GetProject(AccessMode.CurrentFirst);
        }

        // 
        public static string GetCurrentProject()
        {
            return BasicServiceManager.GetProject(AccessMode.Current);
        }

        // 
        public static string GetDefaultProject()
        {
            return BasicServiceManager.GetProject(AccessMode.Default);
        }

        // 
        public static string GetDemoProject()
        {
            return Path.Combine(Environment.CurrentDirectory, "Demo/Demo.proj");
        }

        // 
        private static string GetProject(AccessMode accessMode)
        {
            if (File.Exists(AppCommonString.StartupCfgPath))
            {
                JsonRepository jsonRepository = new JsonRepository(AppCommonString.StartupCfgPath);
                jsonRepository.Load(typeof(Startup));
                Startup startup = jsonRepository.Model as Startup;
                if (startup != null)
                {
                    if (accessMode == AccessMode.Default)
                    {
                        return startup.DefaultProject;
                    }
                    if (accessMode == AccessMode.Current)
                    {
                        return startup.CurrentProject;
                    }
                    if (!string.IsNullOrEmpty(startup.CurrentProject))
                    {
                        return startup.CurrentProject;
                    }
                    return startup.DefaultProject;
                }
            }
            return string.Empty;
        }

        //
        public static void SetCurrentProject(string projectPath)
        {
            BasicServiceManager.SetProject(AccessMode.Current, projectPath);
        }

        // 
        public static void SetDefaultProject(string projectPath)
        {
            BasicServiceManager.SetProject(AccessMode.Default, projectPath);
        }

        // 
        public static string EntryAssemblyDirectory
        {
            get
            {
                Assembly entryAssembly = Assembly.GetEntryAssembly();
                if (!(entryAssembly == null))
                {
                    return Path.GetDirectoryName(entryAssembly.Location);
                }
                return "";
            }
        }

        //
        private static void SetProject(AccessMode accessMode, string projectPath)
        {
            Startup startup = null;
            JsonRepository jsonRepository = new JsonRepository(AppCommonString.StartupCfgPath);
            if (File.Exists(AppCommonString.StartupCfgPath))
            {
                jsonRepository.Load(typeof(Startup));
                startup = (jsonRepository.Model as Startup);
            }
            if (startup == null)
            {
                startup = new Startup();
            }
            switch (accessMode)
            {
                case AccessMode.Current:
                    startup.CurrentProject = projectPath;
                    break;
                case AccessMode.Default:
                    startup.DefaultProject = projectPath;
                    break;
            }
            jsonRepository.Model = startup;
            jsonRepository.Save();
        }

        //
        public static Dictionary<string, ModuleSysInfo> GetModuleSysInfos()
        {
            if (_moduleSysInfos == null)
            {
                _moduleSysInfos = new Dictionary<string, ModuleSysInfo>();
                foreach (ModuleSysInfo moduleSysInfo in ProjectInfo.GetAllPlugIns())
                {
                    _moduleSysInfos.Add(moduleSysInfo.Name, moduleSysInfo);
                }
            }
            return _moduleSysInfos;
        }

        private static IProjService _serverHost;
        private static Dictionary<string, ModuleSysInfo> _moduleSysInfos;
    }
}
